<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use DB;
use Hash;

class UserController extends Controller
{
   
    /**
     * 用户首页
     *
     */
    public function index()
    {
       return view('admin.user.index');
    }


    /**
     * 用户列表页
     *
     */
    public function lists(Request $request)
    {
       //return view('admin.user.lists');
         //1 每页显示的数量 调整
        //2 检索的条件
        $users = User::orderBy('id','desc')
                ->where(function($query) use ($request){
                    //获取关键字
                    $keyword = $request->input('keyword');
                    //检测参数
                    if(!empty($keyword)) {
                        $query->where('username','like','%'.$keyword.'%');
                    }
                })
                ->paginate($request->input('num', 10));

        //分配变量 解析模板
        return view('admin.user.lists', ['users'=>$users,'request'=>$request]);
        // assign('users', $users); $this->display('index');
    }



     //添加用户页面显示
    public function add()
    {
        return view('admin.user.add');
    }

    /**
     * 处理用户提交的插入动作
     */
    public function insert(Request $request)
    {
        //验证表单
		 $this->validate($request, [
			'username' => 'required|regex:/\w{8,30}/',
			'email' => 'required|email',
			'password' => 'required|regex:/\w{8,30}/|same:repassword',
		 ],['username.required'=>'用户名不能省略',
			'username.regex'=>'用户名长度不符合要求，请填写8-25位数字或字母或汉字',
			'email.required'=>'邮箱不能为空',
			'emai.email'=>'邮箱格式不正确',
			'password.required'=>'密码不能为空',
			'password.regex'=>'密码长度不符合要求，请填写8-25位数字或字母',
			'password.same'=>'密码与确认密码不一致！',
		 ]);
		

		//数据的插入
		$user=new User;
		$user -> username = $request ->input('username');
		$user -> email = $request ->input('email');
		$user -> password = Hash::make($request ->input('password'));
		$user -> intro = $request ->input('intro');
		

		//处理图片上传
		if($request->hasFile('profile')){
			//文件存放目录
			$path= './Upload/img/'.date('Ymd');
			//获取后缀
			$suffix=$request->file('profile')-> getClientOriginalExtension();
			//文件名称
			$fileName=time().'-'.rand(100000,999999).'.'.$suffix;
			$request->file('profile')->move($path,$fileName);
			$user->profile=trim($path.'/'.$fileName,'.');
		
		}
		if($user->save()){
			return redirect('/admin')->with('info','添加成功');
		}else{
			return back()->with('info','添加失败');
		}

    }

    
	public function edit($id){
		 //读取用户的信息
        $user = User::findOrFail($id);

        //展现用户的信息
        return view('admin.user.edit', ['user'=>$user]);
	}

    /**
     * 处理用户的更新操作
     */
	public function update(Request $request){
		 //读取用户的模型  
        $user = User::findOrFail($request->input('id'));
        $user -> username = $request->input('username');
        $user -> email = $request->input('email');
        $user -> intro = $request->input('intro');

        if ($request->hasFile('profile')) {
            //文件的存放目录
            $path = './Uploads/img/'.date('Ymd');
            //获取后缀
            $suffix = $request->file('profile')->getClientOriginalExtension();
            //文件的名称
            $fileName = time().rand(100000, 999999).'.'.$suffix;
            $request->file('profile')->move($path, $fileName);
            $user -> profile = trim($path.'/'.$fileName,'.');
        }

        if($user->save()) {
            return redirect('/admin/user/index')->with('info','更新成功');
        }else{
            return back()->with('info','更新失败');
        }

	}



	public function query(){
		return view('admin.user.query');
	}
    
     /**
     * 用户的显示
     */
    public function show($id)
    {
        //读取用户的信息
        $user = User::findOrFail($id);

        //展现用户的信息
        return view('admin.user.show', ['user'=>$user]);
    }


    public function queryByName(Request $request){
        //验证表单
         $this->validate($request, [
            'username' => 'required|regex:/\w{8,30}/',
         ],['username.required'=>'用户名不能为空',
            'username.regex'=>'用户名长度不符合要求，请填写8-25位数字或字母或汉字',
         ]);

        //读取用户的信息
        $user = new User;
        $name = $request->input('username');
        $user = DB::table('users')->where('username', $name)->first(); 
        if(!empty($user)) {
             //分配变量 解析模板
             return view('admin.user.show', ['user'=>$user]);
        }else{
            return back()->with('info','查找失败');
        }
    }


    /**
     * 删除操作
     */
    public function delete($id)
    {
        //创建模型
        $user = User::findOrFail($id);
        //读取用户的头像信息
        $profile=$user->profile;
        $path='.'.$profile;
        if(file_exists($path)) {
            unlink($path);
        }
        //删除
        if($user->delete()){
            return back()->with('info','删除成功');
        }else{
            return back()->with('info','删除失败');
        }
    }


    
}
